namespace Landscape.Business.Reporting
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using Telerik.Reporting;
    using Telerik.Reporting.Drawing;
    using Landscape.Business.Common;

    /// <summary>
    /// Summary description for IncidentFeedback.
    /// </summary>
    public partial class IncidentFeedback : Telerik.Reporting.Report
    {
        public Guid IncidentID
        { get; set; }

        public IncidentFeedback()
        {
            //
            // Required for telerik Reporting designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        void IncidentFeedback_NeedDataSource(object sender, System.EventArgs e)
        {
            string iDetCmd =
                        @"SELECT EI.ID, EI.TITLE, (CASE WHEN EI.IS_INCIDENT = 1 THEN 'Incident' ELSE 'Dril' END) AS INCIDENT_TYPE, (CASE WHEN EI.IS_CLOSED = 1 THEN 'Yes' ELSE 'No' END) AS IS_CLOSED, 
                        (CASE WHEN EI.CLOSED_DATE IS NOT NULL THEN TO_CHAR(EI.CLOSED_DATE, 'DD-MON-YYYY') ELSE '-' END) CLOSED_DATE, (CASE WHEN EI.CREATED_DATE IS NOT NULL THEN TO_CHAR(EI.CREATED_DATE, 'DD-MON-YYYY') ELSE '-' END) CREATED_DATE,
                        (UM1.FIRST_NAME || ' '|| UM1.LAST_NAME) AS CREATED_BY, (CASE WHEN EI.CLOSED_BY <>'00000000000000000000000000000000' THEN (TO_CHAR(UM2.FIRST_NAME) || ' '|| TO_CHAR(UM2.LAST_NAME)) ELSE '-' END) AS CLOSED_BY,
                        V.NAME AS VSL_NAME
                        FROM EMERGENCY_INCIDENT EI, USER_MASTER UM1, USER_MASTER UM2, VESSEL V
                        WHERE UM1.ID = EI.CREATED_BY AND EI.CLOSED_BY = UM2.ID(+) AND V.ID = EI.VESSEL_ID AND EI.ID = '{0}'";

            var oraGuid = GUIDField.CreateFromMicrosoftGuid(this.IncidentID);

            var qry = string.Format(iDetCmd, oraGuid.ToOracleString().ToUpper());
            this.IDet.SelectCommand = qry;

            string feedbackCmd =
                @"SELECT EF.TYPE, EF.COMMENTS, (CASE WHEN EF.TYPE = 'CORRECTIVE_ACTION' THEN TO_CHAR(EF.CORRECTIVE_ACTION) ELSE '-' END) AS CORRECTIVE_ACTION,
                    (CASE WHEN EF.TYPE = 'CORRECTIVE_ACTION' THEN (TO_CHAR(UM1.FIRST_NAME) || ' ' || TO_CHAR(UM1.LAST_NAME)) ELSE '-' END) AS RESPONDER, 
                    (CASE WHEN EF.TYPE = 'CORRECTIVE_ACTION' THEN (TO_CHAR(UM2.FIRST_NAME) || ' ' || TO_CHAR(UM2.LAST_NAME)) ELSE '-' END) AS REVIEWER,
                    (CASE WHEN EF.TYPE = 'CORRECTIVE_ACTION' THEN TO_CHAR(EF.RESPONSE_DUE_DATE, 'DD-MON-YYYY') ELSE '-' END) AS EXPECTED_DATE,
                    (TO_CHAR(UM3.FIRST_NAME) || ' ' || TO_CHAR(UM3.LAST_NAME)) AS CREATED_BY, TO_CHAR(EF.CREATED_DATE, 'DD-MON-YYYY') AS CREATED_DATE
                    FROM EMERGENCY_FEEDBACK EF, USER_MASTER UM1, USER_MASTER UM2, USER_MASTER UM3
                    WHERE UM1.ID(+) = EF.ASSIGNED_TO AND UM2.ID(+) = EF.REVIEW_TO AND UM3.ID = EF.CREATED_BY AND EF.EMERGENCYINCIDENT_ID = '{0}'";

            this.FeedbackData.SelectCommand = string.Format(feedbackCmd, oraGuid.ToOracleString().ToUpper());
            this.DataSource = this.IDet;
        }
    }
}